package com.powroznik.tracker.engine.util;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import com.powroznik.tracker.engine.data.Location;

@Produces("application/json")
@Provider
public class LocationMessageBodyWriter implements MessageBodyWriter<Location> {
	@Override
	public long getSize(Location location, Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mt) {
		return -1;
	}

	@Override
	public boolean isWriteable(Class<?> c, Type t, Annotation[] as, MediaType mt) {
		return Location.class.isAssignableFrom(c);
	}

	@Override
	public void writeTo(Location location, Class<?> c, Type t, Annotation[] as,
			MediaType mt, MultivaluedMap<String, Object> mm, OutputStream os)
			throws IOException, WebApplicationException {
		try {
			ConversionUtils.writeString(os,
					ConversionUtils.writeLocation(location));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
